package com.pic4493.net;

import com.pic4493.comm.Session;
import com.pic4493.entities.DJson;
import com.pic4493.entities.EUser;
import com.pic4493.utils.UtiNet;
import com.pic4493.utils.UtiOffLineManager;

import android.os.Message;
import android.util.Log;

public final class NetClientJ
{
   private static final int PAGE_SIZE = 20;

   /**
    * 注册
    * 
    * @param user
    * @return
    */
   public static DJson SignUp(EUser user)
   {
	  DJson json = new DJson();
	  try
	  {
		 json.getHead().setApiCode("api_00001");
		 json.getBody().put("username", user.getUserName());
		 json.getBody().put("passwd", user.getPassWord());
		 json.getBody().put("nickname", user.getNickName());
		 return sendMessage(json);
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "NetClientJ_SignUp(user) " + e);
	  }
	  return null;
   }

   /**
    * 登录
    * 
    * @param user
    * @return
    */
   public static DJson Login(EUser user)
   {
	  DJson json = new DJson();
	  try
	  {
		 json.getHead().setApiCode("api_00002");
		 json.getBody().put("username", user.getUserName());
		 json.getBody().put("passwd", user.getPassWord());
		 return sendMessage(json);
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "NetClientJ_Login(user) " + e);
	  }
	  return null;
   }

   /**
    * 上传头像
    * 
    * @return
    */
   public static DJson UploadFaceImage(String imgString)
   {
	  DJson json = new DJson();
	  try
	  {
		 json.getHead().setApiCode("api_00003");
		 json.getBody().put("avatars", imgString);
		 return sendMessage(json);
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "NetClientJ_UploadFaceImage() " + e);
	  }
	  return null;
   }

   /**
    * 获取用户首页订阅列表
    * 
    * @return
    */
   public static DJson GetHomeSubscribes()
   {
	  DJson json = new DJson();
	  try
	  {
		 json.getHead().setApiCode("api_00004");
		 return sendMessage(json);
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "NetClientJ_GetHomeSubscribes(userid) " + e);
	  }
	  return null;
   }

   /**
    * 根据用户ID获取收藏列表
    * 
    * @return
    */
   public static DJson GetUserFavorites(int pageIndex)
   {
	  DJson json = new DJson();
	  try
	  {
		 json.getHead().setApiCode("api_00005");
		 json.getHead().setPage(pageIndex);
		 return sendMessage(json);
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "NetClientJ_GetFavoritesByUserId() " + e);
	  }
	  return null;
   }

   /**
    * 添加用户订阅
    * 
    * @param sid
    * @return
    */
   public static DJson AddSubscribe(String sid)
   {
	  DJson json = new DJson();
	  try
	  {
		 json.getHead().setApiCode("api_00008");
		 json.getBody().put("sid", sid);
		 return sendMessage(json);
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "NetClientJ_AddSubscribe(sid) " + e);
	  }
	  return null;
   }

   /**
    * 获取壁纸列表
    * @return
    */
   public static DJson GetWallPaperList(int pageIndex)
   {
	  DJson json = new DJson();
	  try
	  {
		 json.getHead().setApiCode("api_00009");
		 json.getHead().setPage(pageIndex);
		 return sendMessage(json);
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "NetClientJ_GetWallPaperList() " + e);
	  }
	  return null;
   }

   /**
    * 根据相册ID获取相册里面的图片
    * 
    * @param aid
    * @return
    */
   public static DJson GetPhotosByAlbumId(String aid)
   {
	  DJson json = new DJson();
	  try
	  {
		 json.getHead().setApiCode("api_00010");
		 json.getBody().put("aid", aid);
		 return sendMessage(json);
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "NetClientJ_DeleteSubscribe(sid) " + e);
	  }
	  return null;
   }

   /**
    * 通过专题ID获取相册列表
    * 
    * @param sid
    * @param orderBy
    * @return
    */
   public static DJson GetAlbumsBySubjectId(String sid, String orderBy, int pageIndex)
   {
	  DJson json = new DJson();
	  try
	  {
		 json.getHead().setApiCode("api_00011");
		 json.getHead().setPage(pageIndex);
		 json.getBody().put("sid", sid);
		 json.getBody().put("orderby", orderBy);
		 return sendMessage(json);
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "NetClientJ_GetAlbumsBySubjectId(sid, orderBy) " + e);
	  }
	  return null;
   }

   /**
    * 根据图片ID获取评论列表
    * 
    * @param picId
    * @return
    */
   public static DJson GetReviewsByAlbumId(String aid)
   {
	  DJson json = new DJson();
	  try
	  {
		 json.getHead().setApiCode("api_00014");
		 json.getBody().put("aid", aid);
		 return sendMessage(json);
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "NetClientJ_GetReviewsByAlbumId(aid) " + e);
	  }
	  return null;
   }

   /**
    * 提交评论
    * 
    * @param pid
    * @param content
    * @param parentId
    * @param nickname
    * @return
    */
   public static DJson UploadReview(String pid, String content, String parentId, String nickname)
   {
	  DJson json = new DJson();
	  try
	  {
		 json.getHead().setApiCode("api_00016");
		 json.getBody().put("aid", pid);
		 json.getBody().put("content", content);
		 json.getBody().put("parentid", parentId);
		 json.getBody().put("nickname", nickname);
		 return sendMessage(json);
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "NetClientJ_UploadReview(pid, content, parentId, nickname) " + e);
	  }
	  return null;
   }

   /**
    * 获取用户消息
    * 
    * @return
    */
   public static DJson GetMessagesByUserId()
   {
	  DJson json = new DJson();
	  try
	  {
		 json.getHead().setApiCode("api_00018");
		 return sendMessage(json);
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "NetClientJ_GetMessagesByUserId() " + e);
	  }
	  return null;
   }

   /**
    * 回复消息
    * 
    * @param mid
    * @param nickname
    * @param message
    * @return
    */
   public static DJson ReplyMessage(String mid, String nickname, String message)
   {
	  DJson json = new DJson();
	  try
	  {
		 json.getHead().setApiCode("api_00019");
		 json.getBody().put("pmid", mid);
		 json.getBody().put("nickname", nickname);
		 json.getBody().put("message", message);
		 return sendMessage(json);
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "NetClientJ_DeleteMessageByMessageId(mid) " + e);
	  }
	  return null;
   }

   /**
    * 删除用户订阅
    * 
    * @param sid
    * @return
    */
   public static DJson DeleteSubscribe(String sid)
   {
	  DJson json = new DJson();
	  try
	  {
		 json.getHead().setApiCode("api_00020");
		 json.getBody().put("sid", sid);
		 return sendMessage(json);
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "NetClientJ_DeleteSubscribe(sid) " + e);
	  }
	  return null;
   }

   /**
    * 根据消息ID删除消息
    * 
    * @param mid
    * @return
    */
   public static DJson DeleteMessageByMessageId(String mid)
   {
	  DJson json = new DJson();
	  try
	  {
		 json.getHead().setApiCode("api_00021");
		 json.getBody().put("mid", mid);
		 return sendMessage(json);
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "NetClientJ_DeleteMessageByMessageId(mid) " + e);
	  }
	  return null;
   }

   /**
    * 提交建议
    * 
    * @param content
    * @return
    */
   public static DJson Advise(String content)
   {
	  DJson json = new DJson();
	  try
	  {
		 json.getHead().setApiCode("api_00022");
		 json.getBody().put("nickname", Session.getInstance().getUser().getNickName());
		 json.getBody().put("content", content);
		 return sendMessage(json);
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "NetClientJ_Advise(user) " + e);
	  }
	  return null;
   }

   /**
    * 检查新版本
    * 
    * @return
    */
   public static DJson CheckNewVersion()
   {
	  DJson json = new DJson();
	  try
	  {
		 json.getHead().setApiCode("api_00024");
		 return sendMessage(json);
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "NetClientJ_CheckNewVersion() " + e);
	  }
	  return null;
   }

   /**
    * 获取每日一图
    * @return
    */
   public static DJson GetSplash2Photo()
   {
	  DJson json = new DJson();
	  try
	  {
		 json.getHead().setApiCode("api_00026");
		 return sendMessage(json);
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "NetClientJ_GetWallPaper() " + e);
	  }
	  return null;
   }

   /**
    * 添加收藏
    * 
    * @param picId
    * @return
    */
   public static DJson AddToFavorites(String picId)
   {
	  DJson json = new DJson();
	  try
	  {
		 json.getHead().setApiCode("api_00027");
		 json.getBody().put("pid", picId);
		 return sendMessage(json);
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "NetClientJ_AddToFavorites(picId) " + e);
	  }
	  return null;
   }

   /**
    * 删除收藏
    * 
    * @param picId
    * @return
    */
   public static DJson DeleteFromFavorites(String picId)
   {
	  DJson json = new DJson();
	  try
	  {
		 json.getHead().setApiCode("api_00028");
		 json.getBody().put("pid", picId);
		 return sendMessage(json);
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "NetClientJ_DeleteFromFavorites(picId) " + e);
	  }
	  return null;
   }

   /**
    * 获取每日新图
    * 
    * @return
    */
   public static DJson GetEveryDayPics(int pageIndex)
   {
	  DJson json = new DJson();
	  try
	  {
		 json.getHead().setApiCode("api_00029");
		 json.getHead().setPage(pageIndex);
		 return sendMessage(json);
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "NetClientJ_GetEveryDayPics() " + e);
	  }
	  return null;
   }

   /**
    * 获取用户订阅列表(管理界面)
    * 
    * @return
    */
   public static DJson GetUserSubscribes()
   {
	  DJson json = new DJson();
	  try
	  {
		 json.getHead().setApiCode("api_00030");
		 return sendMessage(json);
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "NetClientJ_GetUserSubscribes() " + e);
	  }
	  return null;
   }

   /**
    * 点赞
    * 
    * @param picId
    * @return
    */
   public static DJson Zan(String picId)
   {
	  DJson json = new DJson();
	  try
	  {
		 json.getHead().setApiCode("api_00031");
		 json.getBody().put("pid", picId);
		 return sendMessage(json);
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "NetClientJ_Zan(picId) " + e);
	  }
	  return null;
   }
   
   /**
    * 
    * @param openId
    * @param nickName
    * @param avatarsUrl
    * @return
    */
   public static DJson QQLogin(String openId,String avatarsUrl,String nickName)
   {
	  DJson json=new DJson();
	  try
	  {
		 json.getHead().setApiCode("api_00032");
		 json.getBody().put("openid", openId);
		 json.getBody().put("avatars", avatarsUrl);
		 json.getBody().put("username", nickName);
		 return sendMessage(json);
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "NetClientJ_QQLogin(openId, nickName, avatarsUrl) " + e);
	  }
	  return null;
   }

   // =============================================================================================

   public static DJson sendPic(byte[] pic)
   {
	  try
	  {
		 String str = HttpAccessor.sendMessage("http://api.4493.com/api.php?m=home&c=tx", pic);
		 if (str != null && !str.equals(""))
		 {
			return DJson.createReceiveDJson(str);
		 }
		 else
		 {
			return null;
		 }
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "NetClientJ_sendPic(pic) " + e);
	  }
	  return null;
   }

   /**
    * 发送数据
    * 
    * @param sendData
    * @return
    */
   private static DJson sendMessage(DJson sendData)
   {
	  try
	  {
		 if (UtiNet.isConnectingToInternet())// 联网
		 {
			String jStr = HttpAccessor.sendMessage(Session.getInstance().getPostUrl(), sendData.toBytes());
			if (jStr != null && !jStr.equals(""))
			{
			   UtiOffLineManager.getInstance().saveOffLineData(sendData.toJson(), jStr);
			   return DJson.createReceiveDJson(jStr);
			}
			else
			{
			   return null;
			}
		 }
		 else
		 // 断网
		 {
			String jStr = UtiOffLineManager.getInstance().getOffLineData(sendData.toJson());
			if (jStr != null && !jStr.equals(""))
			{
			   return DJson.createReceiveDJson(jStr);
			}
			else
			{
			   return null;
			}
		 }
	  }
	  catch (Exception e)
	  {
		 e.printStackTrace();
	  }
	  return null;
   }

   /**
    * 发送数据
    * 
    * @param sendData
    * @return
    */
   private static DJson sendMessage(DJson sendData, int TimeOut)
   {
	  try
	  {
		 String jStr = HttpAccessor.sendMessage(Session.getInstance().getPostUrl(), TimeOut, sendData.toBytes());
		 if (jStr != null && !jStr.equals(""))
		 {
			return DJson.createReceiveDJson(jStr);
		 }
		 else
		 {
			return null;
		 }
	  }
	  catch (Exception e)
	  {
		 e.printStackTrace();
	  }
	  return null;
   }
}
